package com.minshenglife.zanzan.service.impl;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.power.GroupPowerDao;
import com.minshenglife.zanzan.entity.power.GroupPower;
import com.minshenglife.zanzan.result.GroupPowerResult;
import com.minshenglife.zanzan.service.GroupPowerService;

/**
 * 工作内容service
 * 
 * @author huodd
 * 
 */
@Component
@Transactional
public class GroupPowerServiceImpl implements GroupPowerService {
	Logger log = LoggerFactory.getLogger(GroupPowerServiceImpl.class);
	@Resource
	GroupPowerDao groupPowerDao;

	@Override
	public String findByViewEmpId(String viewEmpId) {
		String canBeReviewUser = "";
		GroupPower groupPower = new GroupPower();
		groupPower.setViewEmpId(viewEmpId);
		List<GroupPower> powers = groupPowerDao.findByExample(groupPower);
		if(powers.size()==0)
			return null;
		for (GroupPower sgroupPower : powers) {
			canBeReviewUser += sgroupPower.getBeViewEmpId() + "|";
		}
		return canBeReviewUser;
	}
	
	@Override
	public Map<String,String> findAllGroupPower() {
		
		List<GroupPowerResult> aGroupPowers = groupPowerDao.findAllGroupPower();
		if(aGroupPowers==null||aGroupPowers.size()==0)
			return null;
		Map<String,String> groupMap = new LinkedHashMap<String, String>();
		for(GroupPowerResult result:aGroupPowers){
			String key = result.getViewEmpId()+"("+result.getViewEmpName()+"["+result.getViewEmpPostId()+"])";
			if(!groupMap.containsKey(key)){
				String value=result.getBeViewEmpId()+"("+result.getBeViewEmpName()+"["+result.getBeViewEmpPostId()+"])|";
				if(value.contains("ALL")){
					value="ALL";
				}
				groupMap.put(key, value);
			}else{
				String value = groupMap.get(key)+result.getBeViewEmpId()+"("+result.getBeViewEmpName()+"["+result.getBeViewEmpPostId()+"])|";
				groupMap.put(key, value);
			}
		}
		return groupMap;
	}

	@Override
	public void deleteViewEmpId(String viewEmpId) {
		groupPowerDao.deleteViewEmpId(viewEmpId);
	}

	@Override
	public Map<String, String> findGroupPowerByViewEmpId(String viewEmp) {
		String viewEmpId = viewEmp.substring(0,viewEmp.indexOf("("));
		List<GroupPowerResult> thisGroupPowers = groupPowerDao.findGroupPowerByViewEmpId(viewEmpId);
		if(thisGroupPowers==null||thisGroupPowers.size()==0)
			return null;
		Map<String,String> groupMap = new HashMap<String, String>();
		groupMap.put("key", viewEmp.substring(0,viewEmp.length()-1));
		String value="";
		for(GroupPowerResult result:thisGroupPowers){
			value+=result.getBeViewEmpId()+"|";
		}
		groupMap.put("value", value);
		return groupMap;
	}

	@Override
	public void add(GroupPower groupPower) {
		groupPowerDao.add(groupPower);
	}

	@Override
	public String findByViewEmpIdAndViewType(String viewEmpId, String viewType) {
		String canBeReviewUser = "";
		Map<String, String> map = new HashMap<String, String>();
		map.put("viewEmpId", viewEmpId);
		map.put("viewType", viewType);
		List<GroupPower> powers = groupPowerDao.findGroupPowerByViewEmpIdAndViewType(map);
		if(powers.size()==0)
			return null;
		for (GroupPower sgroupPower : powers) {
			canBeReviewUser += sgroupPower.getBeViewEmpId() + "|";
		}
		return canBeReviewUser;
	}

	@Override
	public String findByViewEmpIdAndViewTypeAndPostId(String viewEmpId,
			String viewType, String postId) {
		if(viewEmpId==null||viewType==null||postId==null||viewEmpId.trim().length()==0||viewType.trim().length()==0||postId.trim().length()==0)
			return "";
		String canBeReviewUser = "";
		Map<String, String> map = new HashMap<String, String>();
		map.put("viewEmpId", viewEmpId);
		map.put("viewType", viewType);
		map.put("postId", postId);
		List<GroupPower> powers = groupPowerDao.findGroupPowerByViewEmpIdAndViewTypeAndPostId(map);
		if(powers.size()==0)
			return null;
		for (GroupPower sgroupPower : powers) {
			canBeReviewUser += sgroupPower.getBeViewEmpId() + "|";
		}
		return canBeReviewUser;
	}

}
